package com.mark.springsecurity.controller;

import com.mark.springsecurity.dto.req.SysAuthorityCmd;
import com.mark.springsecurity.dto.resp.*;
import com.mark.springsecurity.service.AuthorityService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/Authorities")
public class AuthorityController {
    private final AuthorityService authorityService;

    public AuthorityController(AuthorityService authorityService) {
        this.authorityService = authorityService;
    }

    @PostMapping()
    public ResponseEntity<Msg<Object>> save(@RequestBody SysAuthorityCmd sysAuthorityCmd) {
        authorityService.save(SysAuthorityCmd.buildSysAuthority(sysAuthorityCmd));
        Msg<Object> msg = new Msg<>(MessageType.MSG_TYPE_SUCCESS, null, null);
        return new ResponseEntity<>(msg, HttpStatus.OK);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Msg<SysAuthorityDto>> getOne(@PathVariable Long id) {
        Msg<SysAuthorityDto> msg = new Msg<>(MessageType.MSG_TYPE_SUCCESS, null, SysAuthorityDto.build(authorityService.getOne(id)));
        return new ResponseEntity<>(msg, HttpStatus.OK);
    }
}
